<?php
/**
 * Runs in response to a form from view.php. Adds an item to the
 * given resourcepage instance, then redirects back to view with
 * editing mode turned on for that item.
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 *//** */
  
    require_once("module_resourcepage.php");
    $DASHBOARD_COUNTER=DASHBOARD_RESOURCEPAGE_EDIT;
    $module=module_resourcepage::$inst;
 
    $id=required_param('id',PARAM_INT);    // Course Module ID
    $itemtype=required_param('itemtype',PARAM_ALPHA);
    $scrollpos=optional_param('scrollpos',0,PARAM_INT);
    
    $module->obtain_view_info($id,null,$cm,$course,$resourcepage);
    
    // Security checks
    $module->verify_edit_action($cm);
    
    // Add new item
    switch($itemtype) {
        case 'weblink' :
            $added=$module->add_item('weblink',true,null,null,null,'Title','',null,'http://',null,null);
            break;
        case 'file' :
            $added=$module->add_item('file',true,null,null,null,'Title','',null,null,null,null);
            break;          
        case 'text' :
            $added=$module->add_item('text',true,null,null,null,'Heading','',null,null,null,null);
            break;
        case 'crosslink' :
            $added=$module->add_item('crosslink',true,null,null,null,null,'',null,null,null,null);
            break;
        case 'forumlink' :
            $added=$module->add_item('forumlink',true,null,null,null,null,'',null,null,null,null);
            break;           
        case 'section' :
            $added = $module->add_container('_section',$resourcepage->id);
            break;
        default:
            error('[Bug or hack] Unsupported item type '.$itemtype);
    }

    // Add item or subcontainer to parent container
    if ($itemtype != 'section') {
        
        // Add item to container
        $module->add_container_item($resourcepage->containerid,$added);         
    } else {
        
        // New section - Add subcontainer to overall parent container
        $module->add_container_subcontainer($resourcepage->containerid, $added);       
    }
     
    // Log and return to view
    add_to_log($course->id, "resourcepage", "additem", "view.php?id=$cm->id",
        "Added $itemtype #".$added,$cm->id);
    if ($itemtype != 'section') {
        redirect('view.php?id='.$cm->id.'&creating=1&item='.$added.($scrollpos ? '&scrollpos='.$scrollpos : ''));
    } else {

        // New section - No additional creation processing required
        redirect('view.php?id='.$cm->id.'&creating=1&item=c'.$added.($scrollpos ? '&scrollpos='.$scrollpos : ''));
    }
 
?>